require(['highcharts', 'reportCommon'], function () {
    var drvId, drvName, titleStr = "", dateType = "02";

    function driverChange(event, treeId, treeNode) {
        if (treeNode.type === 'DRIVER') {
            drvId = treeNode.id;
            drvName = treeNode.name;
            //标题时间
            titleStr = chartTitle(dateType);
            getDrvBehIdxInfo(drvId, dateType);
            getDrvAnaBehIdx(drvId, dateType);
            getDrvBehRankInfo(drvId, dateType);
        } else {
            $.messager.alert("提示", "请选择驾驶员进行查看。");
        }
    }

    //左侧树
    commonFun.leftTreeNodeClickObj['drvAnaBehaviorInfo'] = driverChange;

    //顶部日期类型
    $(".time-select a").click(function () {
        if (drvId) {
            dateType = $(this).attr('val');
            //标题时间
            titleStr = chartTitle(dateType);
            $(this).addClass("active");
            $(this).siblings().removeClass("active");
            getDrvBehIdxInfo(drvId, dateType);
            getDrvAnaBehIdx(drvId, dateType);
            getDrvBehRankInfo(drvId, dateType);
        }
    });

    //不安全行为次数、行为排名
    function getDrvBehIdxInfo(drvId, dateType) {
        $("p").html("");
        $.ajaxExt({
            url: "/drvAna/getDrvBehIdxInfo.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            $(data).each(function (i, item) {
                if (item.idxType == "060101") {
                    $("#drvBehCnt").html(item.idxValue);
                } else if (item.idxType == "060102") {
                    $("#drvRank").html(item.idxValue);
                }
            });
        });
    }

    //行为数据
    function getDrvAnaBehIdx(drvId, dateType) {
        $.ajaxExt({
            url: "/drvAna/getDrvAnaBehIdx.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            if (data.length > 0) {
                var series = [], category = [];
                var tmpDate = data[0].REPORT_DATE;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].REPORT_DATE == tmpDate) {
                        series.push({"name": data[i].TREND_VAL1, data: []});
                    } else {
                        break;
                    }
                }
                for (i = 0; i < data.length; i += series.length) {
                    xAxisDateFormat(dateType, category, data[i].REPORT_DATE);
                    for (var s = 0; s < series.length; s++) {
                        series[s].data.push(parseInt(data[i + s].TREND_VAL2));
                    }
                }
                drvBehData(titleStr, category, series);
            } else {
                drvBehData("暂无数据", "", "");
            }
        });
    }

    //行为排名 饼图
    function getDrvBehRankInfo(drvId, dateType) {
        $.ajaxExt({
            url: "/drvAna/getDrvBehRankInfo.htm",
            data: {'drvId': drvId, 'dateType': dateType}
        }).done(function (data) {
            var behaviorPct = [],
                trHtml = "<tr><th width='15%'>排名</th><th width='25%'>行为名称</th><th width='15%'>次数</th><th width='20%'>百分比</th></tr>";
            if (data.length > 0) {
                $(data).each(function (i, item) {
                    if (item.IDX_TYPE == "060301") {
                        if (item.RANK_SN <= 5) {
                            //行为排名
                            trHtml += "<tr><td><span class='rank-tip'>" + item.RANK_SN + "</span></td><td>" + item.RANK_VAL1 + "</td><td>" + item.RANK_VAL2 + "</td><td>" + item.RANK_VAL3 + "</td></tr>";
                        }
                        //百分比图
                        behaviorPct.push([item.RANK_VAL1, parseInt(item.RANK_VAL3) || 0]);
                    }
                });
                $("#trHtml").html(trHtml);
                unBehPct(titleStr, behaviorPct);

            } else {
                $("#trHtml").html("<tr><td><span>驾驶员：" + drvName + " 暂无数据</span></td></tr>");
                unBehPct("暂无数据", "");
            }
        });
    }

    function drvBehData(titleStr, category, series) {
        $('#drvBehData').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: "驾驶员：" + drvName + ' ' + titleStr,
                style: {
                    fontFamily: 'sans-serif',
                    fontSize: '14px'
                }
            },
            xAxis: {
                categories: category
            },
            yAxis: {
                min: 0,
                title: {
                    text: ''
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            credits: {
                enabled: false
            },
            legend: {
                align: 'center',
                verticalAlign: 'bottom',
                y: 20,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                borderColor: '#CCC',
                shadow: false
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.x + '</b><br/>' +
                        this.series.name + ': ' + this.y + '<br/>' +
                        'Total: ' + this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal'
                }
            },
            series: series
        });
    }

    //百分比图
    function unBehPct(titleStr, behaviorPct) {
        $("#unBehPct").highcharts({
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: "驾驶员：" + drvName + ' ' + titleStr,
                style: {
                    fontFamily: 'sans-serif',
                    fontSize: '14px'
                }
            },
            tooltip: {
                pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                    }
                }
            },
            credits: {
                enabled: false
            },
            series: [
                {
                    type: 'pie',
                    name: '不文明行为比例',
                    data: behaviorPct
                }
            ]
        });
    }

    //如果左侧树有选择驾驶员则直接查询该驾驶员数据
    business.execFnByLeftTreeSelectNode('DRIVER', driverChange);
});